package com.study.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.study.bean.Users;
import com.study.biz.UsersBiz;
import com.study.dao.UsersDao;

/**
 * Servlet implementation class huiyuanservlet
 */
@WebServlet("/huiyuan")
@MultipartConfig
public class huiyuanservlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private UsersBiz biz= new  UsersBiz();
		
	@Override
	public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		request.setAttribute(getServletInfo(), request.getParameter(getServletName()));		
		int uid=1;
		Object Uid=session.getAttribute("uid");
    	if(Uid!=null){
    		uid=(int)Uid;
    	}
    	System.out.println("************"+uid+"\t"+Uid);
		UsersBiz ubiz=new UsersBiz();
		Users list=biz.selects(uid);
 		request.setAttribute("user", list);
		/* request.setAttribute("user", ulist.get(0)); */
		return "f:huiyuan";
	}
   
    
    public String updatauses(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	Object id=session.getAttribute("uid");
    	response.setContentType("application/json;charset=utf-8");
    	int uid=Integer.parseInt(id.toString());				//鐢ㄦ埛id
    	String nc=request.getParameter("nc");		//鏄电О
    	String sex=request.getParameter("sex");		//鎬у埆
    	String sjh=request.getParameter("sjh");		//鎵嬫満鍙�
    	String password=request.getParameter("password");//瀵嗙爜
    	
    	System.out.println(password);
    	Part part=request.getPart("uphead");
    	
    	
    	String newname="";
		if(!part.getSubmittedFileName().equals("")){
			String fix=part.getSubmittedFileName().substring(part.getSubmittedFileName().lastIndexOf("."));
			//创建路径	
			String path=request.getServletContext().getRealPath("/")+"imgs/liuimgs/";
			File file=new File(path);
			if(!file.exists()){
				file.mkdirs();
			}
			
			//图片新名字
			newname=newFileName()+fix;
			path+="/"+newname;
			part.write(path); 	
		}
		List<Users> ulist= biz.looks(uid); 
    	Users list=biz.selects(uid);
    	System.out.println(list+"涔嬪墠鎷垮埌鐨刲ist");
    	list.setUname(nc);
    	list.setUsex(sex);
    	list.setUphone(sjh);
    	list.setUimg("imgs/liuimgs/"+newname);
    	

		/* String upwd= ulist.get(0).getUpwd(); */
		/* list.setUpwd(upwd); */

		String upwd= ulist.get(0).getUpwd();
		System.out.println(upwd);
		list.setUpwd(upwd);
    	biz.doupdate(list);
    	System.out.println(biz.doupdate(list));
    	System.out.println(list+"淇敼鍙細鐨刲ist");
    	return "r:huiyuan";   
    }
    
    //修改
    public String xiugai(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	Object id=session.getAttribute("uid");
    	int uid=Integer.parseInt(id.toString());	
    	Users list=biz.selects(uid);
    	
    	String mima1=request.getParameter("password1");
    	String mima2=request.getParameter("password2");	
    	String mima3=request.getParameter("password3");	
    	
    	boolean p1=mima1.equals(list.getUpwd());
    	boolean p2=mima2.equals(mima3);
    	
    	Integer str=-1;
    	if(mima1!=null&&mima2!=null&&mima3!=null){
    		if(p1){
    			if(p2){
    				list.setUpwd(mima2);
    				biz.doupdate(list);
    				return "r:shouye?v=loginup";
    			}else{
    				str=2;
    				System.out.println("两次密码不一致");
    			}
    		}else{
    			str=1;
    			System.out.println("原密码错误");
    		}
    	}else{
    		str=0;
    		System.out.println("请填写完整");
    	}
    	
    	return "r:huiyuan?tips="+str;
    }

 
  //创建图片名
   	public String newFileName() {
   		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
   		return sdf.format(new Date())+((int)(Math.random()*9000)+1000);
   	}
 }
		